package com.boluo.web.controller.customer;

import com.boluo.common.annotation.Log;
import com.boluo.common.core.controller.BaseController;
import com.boluo.common.core.domain.AjaxResult;
import com.boluo.common.core.page.TableDataInfo;
import com.boluo.common.enums.BusinessType;
import com.boluo.common.utils.poi.ExcelUtil;
import com.boluo.customer.domain.CustomerInfo;
import com.boluo.customer.domain.CustomerLinkman;
import com.boluo.customer.dto.CustomerInfoDTO;
import com.boluo.customer.service.ICustomerInfoService;
import com.boluo.customer.vo.CustomerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户基本信息Controller
 * 
 * @author SekonoAlpaca
 * @date 2023-05-02
 */
@RestController
@RequestMapping("/customer/customerInfo")
public class CustomerInfoController extends BaseController
{
    @Autowired
    private ICustomerInfoService customerInfoService;

    /**
     * 查询客户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customerInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerInfo customerInfo)
    {
        startPage();
        List<CustomerInfo> list = customerInfoService.selectCustomerInfoList(customerInfo);
        return getDataTable(list);
    }

    /**
     * 查询所属客户列表(不分页)
     */
    @GetMapping("/listAllByIdAndName")
    public AjaxResult listAllByIdAndName()
    {
        List<CustomerInfoDTO> listAllByIdAndName = customerInfoService.listAllByIdAndName();
        return AjaxResult.success(listAllByIdAndName);
    }

    /**
     * 通过复合搜索客户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customerInfo:list')")
    @GetMapping("/listByComplex")
    public TableDataInfo listByComplex(CustomerVO customerInfo)
    {
        startPage();
        List<CustomerInfoDTO> list = customerInfoService.selectCustomerInfoListByComplex(customerInfo);
        return getDataTable(list);
    }

    /**
     * 导出客户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customerInfo:export')")
    @Log(title = "客户基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerInfo customerInfo)
    {
        List<CustomerInfo> list = customerInfoService.selectCustomerInfoList(customerInfo);
        ExcelUtil<CustomerInfo> util = new ExcelUtil<CustomerInfo>(CustomerInfo.class);
        util.exportExcel(response, list, "客户基本信息数据");
    }

    /**
     * 获取客户基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customerInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerInfoService.selectCustomerInfoById(id));
    }


    /**
     *  获取所属客户下的联系人信息
     * @param id
     * @return
     */
    @GetMapping(value = "/selectName/{id}")
    public AjaxResult selectName(@PathVariable("id") Long id)
    {
        List<CustomerLinkman> customerLinkmen = customerInfoService.selectName(id);
        return AjaxResult.success(customerLinkmen);
    }

    /**
     * 新增客户基本信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customerInfo:add')")
    @Log(title = "客户基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerInfo customerInfo)
    {
        return toAjax(customerInfoService.insertCustomerInfo(customerInfo));
    }

    /**
     * 修改客户基本信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customerInfo:edit')")
    @Log(title = "客户基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerInfo customerInfo)
    {
        return toAjax(customerInfoService.updateCustomerInfo(customerInfo));
    }

    /**
     * 删除客户基本信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customerInfo:remove')")
    @Log(title = "客户基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerInfoService.deleteCustomerInfoByIds(ids));
    }
}
